//
//  AddressBook.m
//  TestAfn2
//
//  Created by xujingjing on 16/7/11.
//  Copyright © 2016年 xujingjing. All rights reserved.
//

#import "AddressBook.h"

#import "ZCAddressBook.h"

//iOS9通讯录库
#import <Contacts/Contacts.h>
//iOS9带UI的库
#import <ContactsUI/ContactsUI.h>

#import "ALAddressBookVC.h"  //可传入数据类似通讯录排序

@interface AddressBook ()<UITableViewDataSource,UITableViewDelegate,CNContactPickerDelegate>
{
    
}
@property(nonatomic,strong)NSMutableDictionary*dataDic;
@property(nonatomic,strong)NSArray*dataArray;
@end

@implementation AddressBook

- (void)viewDidLoad {
    [super viewDidLoad];


}
- (IBAction)addressClick:(id)sender {
    
    //添加联系人
    ZCAddressBook *address=[ZCAddressBook shareControl];
    
//    NSArray*titleArray=@[@"大娃",@"二娃",@"三娃",@"四娃",@"五娃",@"七娃"];
//    
//    for (NSString*str in titleArray) {
//        //判断这个人是否已经存在
     NSString *str = @"Anna";
    if ([address existPhone:@"13519266845"] == ABHelperNotExistSpecificContact) {
        
          [address addContactName:str phoneNum:[NSString stringWithFormat:@"13519266845"] ];//phoneNum:电话号码（没有用设置标签会被覆盖） Label:标签
    } ;
    
  // }
    
    
    
}
//自定义联系人,只获取数据源
- (IBAction)customAddressList:(id)sender {
    //获取数据源
    NSDictionary*dic=[[ZCAddressBook shareControl]getPersonInfo];
    self.dataDic=[NSMutableDictionary dictionaryWithDictionary:dic];
    NSLog(@"%@",dic);
    
    //排序后的key值
    NSArray*array=[[ZCAddressBook shareControl]sortMethod];
    self.dataArray=[NSMutableArray arrayWithArray:array];
    NSLog(@"排序后的key值%@",array);
    
    
    [self createTableView];
    
    
}



-(void)createTableView{
    //查询一下tag值为100的
    UITableView*tableView=[self.view viewWithTag:100];
    
    if (tableView) {
        return;
    }
    
    tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 10, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-100) style:UITableViewStylePlain];
    tableView.tag=100;
    tableView.delegate=self;
    tableView.dataSource=self;
    [self.view addSubview:tableView];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataDic[self.dataArray[section]] count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ID"];
    }
    
    NSDictionary*dic=self.dataDic[self.dataArray[indexPath.section]][indexPath.row];
    
    cell.textLabel.text=[NSString stringWithFormat:@"%@%@",dic[@"first"],dic[@"last"]];
    
    cell.detailTextLabel.text=[dic[@"telphone"]firstObject];
    
    
    
    return cell;
    
}

- (IBAction)defineSortAddressBook:(id)sender {
    
   // [self.navigationController pushViewController:[[ALAddressBookVC alloc]init] animated:YES];

}

//获取系统通讯录
- (IBAction)systemAddressClick:(id)sender {
    
    [[ZCAddressBook shareControl]showPhoneViewWithTarget:self Block:^(BOOL isSuccess, NSDictionary *dic) {
        
        NSLog(@"~~~isSuccess= %d%@",isSuccess,dic);
    }];
    
    
}
- (IBAction)sendMessage:(id)sender {
    //发送短信,群发，可以有指定内容
    [[ZCAddressBook shareControl]showSystemMessageToListArray:@[@"18323635092"] Message:[NSString stringWithFormat:@"%@是未满十八岁的宇宙无敌超级美少女",[[UIDevice currentDevice] systemName]] ViewController:self Block:^(int a) {
        NSLog(@"%d",a);
    }];
    
}


-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
{
    NSLog(@"%@~~%@",contact.familyName,contact.givenName);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
